// import generatePDF from "./lib/PDFExport";

import { fix_synonyms } from "./core/utils";
import { Highlight } from "./core/Hightlight";

/* eslint-disable no-unused-vars */
var zoom = 40;
var pageHeight = 1024;
var pageWidth = 768;
var swipe_pages = false;
var format = "mei";
var outputFilename = "output.mei";
var ids = [];
var pdfFormat = "A4";
var pdfOrientation = "portrait";
var savedOptions = undefined;
var customOptions = undefined;
var target = "";
var disabledOptions = [
  "adjustPageHeight",
  "breaks",
  "landscape",
  "pageHeight",
  "pageWidth",
  "mmOutput",
  "footer",
];
var draw_context = {
  // TODO: One draw context per existing score element
  // already on load.
  mei_score: "",
  svg_elem: "",
  view_elem: "",
  layer: "",
  id_prefix: "",
  zoom: 1,
  reductions: [],
};
var _options = {
  url: "", // 必传
  layer: "", // 必传
  inputFrom: "mei", // 默认 mei
};

// eslint-disable-next-line no-undef
window.vrvToolkit = new verovio.toolkit();

export class VerovioTools {
  constructor(options) {
    this.layer_elem = null;
    this.svg_elem = null;
    this.mei = null; //  doc
    this.svg = null; //  svg
    this.options = options || _options;
    this.mei_graph = null; // node节点
    this.page = 1; // 翻页页码

    this.init(options);
  }
  async init(options) {
    var [mei] = await this.loadData(options.url);
    this.render(options.layer);
    this.addHandleEvent(mei);
  }

  async loadData(_xmlUrl) {
    var xmlStringData = await fetch(_xmlUrl).then((response) =>
      response.text()
    );
    var [mei, svg] = this.render_mei(xmlStringData);

    this.mei = mei;
    this.svg = svg;

    return [svg, svg];
  }

  render(container) {
    var _container = document.getElementById(container);
    _container.innerHTML = this.svg;

    this.layer_elem = _container;
    this.svg_elem = this.layer_elem.children[0];
  }

  render_mei(xmlStringData) {
    var svg = "";
    var mei = "";

    var parser = new DOMParser();
    var doc = parser.parseFromString(xmlStringData, "text/xml");
    var mei_file_type = "http://www.music-encoding.org/ns/mei";
    console.log(doc);
    var options = this.getDefaultOptions();
    vrvToolkit.setOptions(options);

    var fileTypeNoMei = doc.documentElement.namespaceURI != mei_file_type;
    // musicXML
    console.log("文件类型是否非 mei ？", fileTypeNoMei);
    if (fileTypeNoMei) {
      this.options.inputFrom = "xml";
      svg = vrvToolkit.loadData(xmlStringData, this.options);
      if (!svg) {
        console.log("Verovio 无法从非MEI文件生成SVG.");
        return false;
      }
      var _xmlStringData = vrvToolkit.getMEI();

      parser = new DOMParser();
      mei = parser.parseFromString(_xmlStringData, "text/xml");
      svg = vrvToolkit.renderToSVG(this.page, {});
    } else {
      this.options.inputFrom = "mei";
      mei = fix_synonyms(doc);
      svg = vrvToolkit.renderData(xmlStringData, this.options);
    }

    return [mei, svg];
  }

  getDefaultOptions() {
    pageHeight = (document.body.clientHeight * 100) / zoom;
    pageWidth = (document.body.clientWidth * 100) / zoom;
    var options = {
      adjustPageHeight: true,
      breaks: "auto",
      inputFrom: this.options.inputFrom,
      mmOutput: false,
      footer: "none",
      pageHeight: pageHeight,
      pageWidth: pageWidth,
      scale: zoom,
    };
    return options;
  }

  loadPage() {
    var new_svg = vrvToolkit.renderToSVG(this.page);

    this.layer_elem.innerHTML = new_svg;
    this.svg_elem = this.layer_elem.children[0];
  }

  prevPage() {
    if (this.page <= 1) {
      return;
    }

    this.page = this.page - 1;
    this.loadPage();
  }

  nextPage() {
    if (this.page >= vrvToolkit.getPageCount()) {
      return;
    }

    this.page = this.page + 1;
    this.loadPage();
  }

  addHandleEvent(mei) {
    this.layer_elem.onclick = (e) =>
      Highlight.hightlight_note(e, this.layer_elem, mei);
  }

  savePDF(_meiData) {
    var meiData = this.mei;

    // 导入会报错
    // generatePDF(meiData);
  }
}
